﻿// <copyright file="FailedRequiredClaimsFaultBodyWriter.cs" company="SharpSTS">
// Copyright (c) 2007, 2008 All Right Reserved, http://sharpsts.com/
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Barry Dorrans</author>
// <email>barryd@idunno.org</email>
// <date>2008-06-11</date>
// <summary>A body writer to construct a FailedRequiredClaims fault.</summary>

namespace SharpSTS
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.ServiceModel.Channels;
    using System.Xml;

    /// <summary>
    /// A body writer to construct a FailedRequiredClaims fault.
    /// </summary>
    internal sealed class FailedRequiredClaimsFaultBodyWriter : BodyWriter
    {
        /// <summary>
        /// The SOAP fault reason.
        /// </summary>
        private readonly string faultReason;

        /// <summary>
        /// A list of claims which caused the fault.
        /// </summary>
        private readonly List<string> claimTypes;

        /// <summary>
        /// Initializes a new instance of the <see cref="FailedRequiredClaimsFaultBodyWriter"/> class.
        /// </summary>
        /// <param name="faultReason">The fault reason.</param>
        /// <param name="claimTypes">The claim types.</param>
        public FailedRequiredClaimsFaultBodyWriter(string faultReason, List<string> claimTypes)
            : base(true)
        {
            this.faultReason = faultReason;
            this.claimTypes = claimTypes;
        }

        /// <summary>
        /// When implemented, provides an extensibility point when the body contents are written.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Xml.XmlDictionaryWriter"/> used to write out the message body.</param>
        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            writer.WriteStartElement("Fault", "http://www.w3.org/2003/05/soap-envelope");
            writer.WriteXmlnsAttribute("ic", Constants.WSIdentity.Namespace.Uri);

            WriteCode(writer);
            this.WriteReason(writer);
            this.WriteDetail(writer);

            writer.WriteEndElement();
        }

        /// <summary>
        /// Writes the code.
        /// </summary>
        /// <param name="writer">The writer.</param>
        private static void WriteCode(XmlWriter writer)
        {
            // <Code>
            //    <Value>Sender</Value>
            //    <Subcode>
            //        <Value xmlns:ic="http://schemas.xmlsoap.org/ws/2005/05/identity">ic:FailedRequiredClaims</Value>
            //    </Subcode>
            // </Code>
            writer.WriteStartElement("Code");
            writer.WriteElementString("Value", "Sender");
            writer.WriteStartElement("Subcode");
            writer.WriteElementString("Value", "ic:FailedRequiredClaims");
            writer.WriteEndElement();
            writer.WriteEndElement();
        }

        /// <summary>
        /// Writes the fault detail.
        /// </summary>
        /// <param name="writer">The writer.</param>
        private void WriteDetail(XmlWriter writer)
        {
            writer.WriteStartElement("Detail");

            foreach (string claimtype in this.claimTypes)
            {
                writer.WriteStartElement("ClaimType", Constants.WSIdentity.Namespace.Uri);
                writer.WriteAttributeString("Uri", claimtype);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }
        
        /// <summary>
        /// Writes the fault reason.
        /// </summary>
        /// <param name="writer">The writer.</param>
        private void WriteReason(XmlWriter writer)
        {
            // <Reason>
            //    <Text xml:lang="en-UK">Could not satisfy required claims in request; construction of token failed</Text>
            // </Reason>
            writer.WriteStartElement("Reason");
                writer.WriteStartElement("Text");
                    writer.WriteAttributeString("xml:lang", CultureInfo.CurrentCulture.ToString());
                    writer.WriteString(this.faultReason);
                writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}
